package com.woniuxy.ams.controller;



import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniuxy.ams.entity.AssessItem;
import com.woniuxy.ams.entity.AssessType;
import com.woniuxy.ams.entity.PageRequest;
import com.woniuxy.ams.entity.ResultEntity;
import com.woniuxy.ams.service.AssessItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;


/**
 * @author Zjp
 */
@RestController
@RequestMapping("/item")
public class AssessItemController {
    @Autowired(required = false)
    AssessItemService assessItemService;


    /**
     * 查询item表的所有数据
     *
     * @return 查询结果
     */
    @RequestMapping("/findAllAssessItem")
    public ResultEntity<?> findAllAssessItem(@RequestBody PageRequest<AssessItem> pageRequest) {
        PageHelper.startPage(pageRequest.getCurrentPage(), pageRequest.getLimit());
        AssessItem data = pageRequest.getData();
        List<AssessItem> assessItems = assessItemService.findAllAssessItem(data);
        System.out.println(assessItems);
        PageInfo<AssessItem> objectPageInfo = new PageInfo<>(assessItems);
        if (assessItems.size() != 0) {
            return new ResultEntity<>(200, "查询成功", objectPageInfo);
        }else {
            return new ResultEntity<>(200, "查询失败,没有此数据", objectPageInfo);
        }
    }

    /**
     * 查询所有无分页
     * @param pageRequest
     * @return
     */
    @RequestMapping("/findAll")
    public ResultEntity<?> findAll(@RequestBody PageRequest<AssessItem> pageRequest) {
        List<AssessItem> assessItems = assessItemService.findAll();
        if (assessItems.size() != 0) {
            return new ResultEntity<>(200, "查询成功", assessItems);
        }else {
            return new ResultEntity<>(200, "查询失败,没有此数据", assessItems);
        }
    }

    /**
     *添加item
     * @param
     * @return
     */
    @RequestMapping("/addAssessItem")
    public ResultEntity<?> addAssessItem(@RequestBody AssessItem assessItem) {
        System.out.println(assessItem);
        boolean b = assessItemService.addAssessItem(assessItem);
        if (b) {
            return new ResultEntity<>(200, "添加成功", null);
        } else {
            return new ResultEntity<>(500, "添加失败", null);
        }
    }

    /**
     * 删除item
     * 此处的删除是将item表中的status字段改为是（是为废弃状态，否为未废弃状态）
     * @param assessItem
     * @return
     */
    @RequestMapping("/deleteAssessItem")
    public ResultEntity<?> deleteAssessItem(@RequestBody AssessItem assessItem) {
        boolean b = assessItemService.deleteAssessItem(assessItem);
        if (b) {
            return new ResultEntity<>(200, "删除成功", null);
        } else {
            return new ResultEntity<>(500, "删除失败", null);
        }
    }

    /**
     * 更改itme表中的数据
     * @param assessItem
     * @return
     */
    @RequestMapping("/updateAssessItem")
    public ResultEntity<?> updateAssessItem(@RequestBody AssessItem assessItem) {
        System.out.println(assessItem);
        boolean b = assessItemService.updateAssessItem(assessItem);
        if (b) {
            return new ResultEntity<>(200, "修改成功", null);
        } else {
            return new ResultEntity<>(500, "修改失败", null);
        }
    }

    /**
     * 根据dept查询数据
     * @param assessItem
     * @return
     */
    @RequestMapping("/findByDept")
    public ResultEntity<List<AssessItem>> findByItemDept(@RequestBody AssessItem assessItem) {
        List<AssessItem> assessTypes = assessItemService.findByItemDept(assessItem);
        if (assessTypes != null && assessTypes.size()!=0) {
            return new ResultEntity<>(200, "查询成功", assessTypes);
        }else {
            return new ResultEntity<>(200, "查询失败,没有此数据", null);
        }
    }


    /**
     * 根据指标名称查询
     * @param assessItem
     * @return
     */
    @RequestMapping("/findByIfPaName")
    public ResultEntity<List<AssessItem>> findByIfPaName(@RequestBody AssessItem assessItem) {
        List<AssessItem> assessTypes = assessItemService.findByIfPaName(assessItem);
        if (assessTypes != null && assessTypes.size()!=0) {
            return new ResultEntity<>(200, "查询成功", assessTypes);
        }else {
            return new ResultEntity<>(200, "查询失败,没有此数据", null);
        }
    }

    /**
     * 根据部门id列表查询
     * @param depts
     * @return ResultEntity<List<AssessItem>>
     */
    @RequestMapping("/findByDepts")
    public ResultEntity<List<AssessItem>> findByDepts(@RequestParam("depts") String depts){
        String[] deptIds = depts.split(",");
        List<String> deptIdList = Arrays.asList(deptIds);
        List<Integer> depts2 = new ArrayList<>();
        for (String s : deptIdList) {
            Integer deptId = Integer.parseInt(s);
            depts2.add(deptId);
        }
        List<AssessItem> assessItems = assessItemService.findByDepts(depts2);
        if (assessItems.size() != 0){
            return new ResultEntity<>(200, "查询成功", assessItems);
        }else {
            return new ResultEntity<>(200, "查询失败", null);
        }

    }

    /**
     * 根据id查询
     * @param assessItem
     * @return
     */
    @RequestMapping("/findById")
    public ResultEntity<List<AssessItem>> findById(@RequestBody AssessItem assessItem) {
        List<AssessItem> assessItems = assessItemService.findById(assessItem);
        if (assessItems != null && assessItems.size() != 0) {
            return new ResultEntity<>(200, "查询成功", assessItems);
        } else {
            return new ResultEntity<>(200, "查询失败,没有此数据", null);
        }

    }
}
